package com.cloudpivot.project.crmprojectnew.service.impl;

import com.cloudpivot.project.crmprojectnew.configuration.CrmConfig;
import com.cloudpivot.project.crmprojectnew.pojo.accountequipment.AccountEquipment;
import com.cloudpivot.project.crmprojectnew.pojo.accountequipment.AccountEquipmentList;
import com.cloudpivot.project.crmprojectnew.service.AccountEquipmentService;
import com.cloudpivot.project.crmprojectnew.service.HttpAPIServiceForCrm;
import com.cloudpivot.project.crmprojectnew.service.SystemusersService;
import com.cloudpivot.project.crmprojectnew.util.CrmNormalUtil;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 客户设备实现类
 */
@Service
@Slf4j
public class AccountEquipmentServiceImpl implements AccountEquipmentService {
    @Autowired
    private CrmConfig crmConfig;
    @Autowired
    private SystemusersService systemusersService;

    @Autowired
    private ObjectMapper objectMapper;

    @Autowired
    private HttpAPIServiceForCrm httpAPIServiceForCrm;

    private static final String APISTR = "new_account_equipments";

    private static final String EntityStr = "new_account_equipment";

    /**
     * 获取一个客户设备信息
     *
     * @param guid
     * @return
     */
    @Override
    public AccountEquipment getAccountEquipment(String guid) {
        //客户设备地址
        if ("".equals(guid) || guid == null) {
            return null;
        } else {
            String requestUrl = String.format("%s%s(%s)", crmConfig.getCrmurl(), APISTR, guid);
            try {
                String responseStr = httpAPIServiceForCrm.doGetCrm(requestUrl);
                AccountEquipment accountEquipment = objectMapper.readValue(responseStr, AccountEquipment.class);

                return accountEquipment;
            } catch (Exception ex) {
                ex.printStackTrace();
                return null;
            }
        }
    }

    /**
     * 获取客户设备List
     *
     * @param userid
     * @return
     */
    @Override
    public AccountEquipmentList getAccountEquipments(String userid, int pageIndex, int pageSize) {
        //客户设备地址
        if ("".equals(userid) || userid == null) {
            return null;
        } else {
            AccountEquipmentList accountEquipmentList = new AccountEquipmentList();
            boolean flag;
            try {
                List<AccountEquipment> tmpList = new ArrayList<>();
                AccountEquipmentList doList = null;
                //创建请求url,带分页
                String requestUrl = CrmNormalUtil.urlEcode(String.format("%s%s?%s", crmConfig.getCrmurl(), APISTR, CrmNormalUtil.getFetchPage(pageIndex, pageSize, EntityStr)));
                log.info("客户设备请求url：" + requestUrl);
                String responseStr = httpAPIServiceForCrm.doGetCrm(requestUrl, userid);
                accountEquipmentList = objectMapper.readValue(responseStr, AccountEquipmentList.class);
                return accountEquipmentList;
            } catch (Exception ex) {
                ex.printStackTrace();
                return null;
            }
        }

    }
}
